package com.hisense.ovcloud.mqtt.warehouse;

public class Constants {
    public final static int RESULT_SUCCESS = 0;
    public final static int RESULT_FAILED = 1;
    public final static String SUPER_ACCOUNT = "999999999999999";

    public final static int STATUS_ONLINE = 1;
    public final static int STATUS_OFFLINE = 2;

    public final static String STATUS_ONLINE_PAYLOAD = "{\"status\":1}";
    public final static String STATUS_OFFLINE_PAYLOAD = "{\"status\":2}";

    public final static String ONLINE_REDIS_PREFIX = "MQTT#";
    public final static String REDIS_NULL_VALUE = "REDIS_NULL_VALUE";

    public final static String AESKEY_PREFIX = "aes_";

    public final static String REST_SCHEMA_HTTP = "http://";
    public static String ENDPOINT_NAME_PATTHEN = "(?<MajorDomainID>\\d+)/(?<SubDomainID>\\d+)/(?<PhysicalID>[0-9a-zA-Z]+)_(?<SecureMode>\\d+)_(?<SignMethod>\\d+)_(?<Timestamp>\\d+)";

    public static class ClientEp {
        public static class SignMethod{
            public final static String HmacMD5 = "1";
            public final static String HmacSHA1 = "2";
            public final static String HmacSHA256 = "3";
        }
    }

    public static class Warehouse {
        public final static int ACTIVATED = 2;
        public final static int NOT_ACTIVATED = 1;
        public final static String WIFI_ID_TO_REPLACE = "__WIFI_ID_TO_REPLACE__";
        public final static String ORIGIN_DEVICE_WAREHOUSE_TABLE_NAME_SUFFIX = "_device_warehouse";

        public final static String STATUS_TO_REPLACE = "__STATUS_TO_REPLACE__";
        public final static String DOMAIN_TO_REPLACE = "__DOMAIN_TO_REPLACE__";
        public final static String TABLE_NAME_TO_REPLACE = "__TABLE_NAME_TO_REPLACE__";

        public final static String SELECT_BY_WIFI_ID = "select * from __DOMAIN_TO_REPLACE___device_warehouse where device_id = '__WIFI_ID_TO_REPLACE__'";

        public final static String UPDATE_STATUS = "update __DOMAIN___device_warehouse set status = __STATUS_TO_REPLACE__, modify_time=UTC_TIMESTAMP() where device_id = '__WIFI_ID_TO_REPLACE__'";

        public final static String INSERT_PREFIX = "INSERT INTO __DOMAIN___device_warehouse (sub_domain, device_id, device_type, public_key, status, code, batch_code, create_time, modify_time) VALUES ";
        public final static String INSERT_SUFFIX = " ON DUPLICATE KEY UPDATE code=values(code), device_id=values(device_id), sub_domain=values(sub_domain), device_type=values(device_type), public_key=values(public_key), " +
                "status=values(status), batch_code=values(batch_code), modify_time=UTC_TIMESTAMP(), create_time=UTC_TIMESTAMP()";

        public final static String CREATE_WAREHOUSE_DB = "create database if not exists warehouse default character set utf8mb4 collate utf8_unicode_ci;";
        public final static String DOMAIN_ID_TO_REPLACE = "__DOMAIN__";

        public final static String CREATE_DEVICE_STATUS_TABLE = """
                   CREATE TABLE IF NOT EXISTS __DOMAIN___device_record (
                    device_id varchar(64) NOT NULL,
                    sub_domain varchar(32) NOT NULL,
                    access_token varchar(32) NOT NULL,
                    access_token_expire datetime NOT NULL,
                    refresh_token varchar(32) NOT NULL,
                    refresh_token_expire datetime NOT NULL,
                    ip_addr varchar(32) NOT NULL,
                    mac varchar(32) NOT NULL,
                    dev_version varchar(64) NOT NULL,
                    mod_version varchar(64) NOT NULL,
                    mod_type varchar(64) NOT NULL,
                    longitude double NOT NULL,
                    latitude double NOT NULL,
                    country varchar(64) NOT NULL,
                    province varchar(64) NOT NULL,
                    city varchar(64) NOT NULL,
                    status int(8) NOT NULL DEFAULT '2',     -- 1:not-activated || 2:activated || (license status). 因为此字段值的高位比特记录了设备的授权状态；低4位记录激活状态。所以比较设备的激活状态时需要先取低4位的值：status&0x0f。
                     street varchar(64) NOT NULL,
                    create_time datetime DEFAULT NULL,
                    modify_time datetime DEFAULT NULL,
                    first_active_time datetime DEFAULT NULL,
                    last_active_time datetime DEFAULT NULL,
                    import_time datetime DEFAULT NULL,
                    heartbeat_time datetime DEFAULT NULL,
                    virtual_unit int(8) NOT NULL DEFAULT '2',			-- 是否为虚拟设备。0：无效；1：虚拟设备；2：真正设备
                    online_status int(8) NOT NULL DEFAULT '2',  -- 1:on-line || 2:off-line
                    PRIMARY KEY (device_id),
                    KEY import_time_index (import_time) USING BTREE,   -- 设备授权页面会基于导入时间查询，此处应对导入时间建索引
                    KEY sub_domain_index (sub_domain) USING BTREE,
                    KEY dev_version_index (dev_version) USING BTREE,     -- 设备列表页面会基于激活状态和固件版本查询，应都加索引
                    KEY status_index (status) USING BTREE
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
                """;

        public final static String CREATE_DEVICE_WAREHOUSE_TABLE = """
                           CREATE TABLE IF NOT EXISTS __DOMAIN___device_warehouse (
                            code bigint(20) NOT NULL DEFAULT '0',       -- 设备入库的批次（如果从来没有入库过，则code==0）
                            device_id varchar(64) NOT NULL,             -- physicalDeviceId of device
                            sub_domain varchar(32) NOT NULL,
                            device_type int(8) NOT NULL DEFAULT '0',	-- 0：NORMAL；1：MASTER。
                            public_key varchar(128) DEFAULT NULL,
                            aes_key varchar(128) DEFAULT NULL,
                            license varchar(128) DEFAULT NULL,          -- (update: 2016/05/25 [new])
                            status int(8) NOT NULL DEFAULT '1',			-- 设备的使用状态。0：无效；1：在用；2：停用；3：删除等。
                            create_time datetime DEFAULT NULL,
                            modify_time datetime DEFAULT NULL,
                            batch_code varchar(64) NOT NULL DEFAULT '0',
                            PRIMARY KEY (device_id),
                            UNIQUE KEY (license) USING HASH             -- (update: 2016/05/25 [new])
                        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
                """;

        public final static String CREATE_DEVICE_PROPERTY_TABLE = """
                    CREATE TABLE IF NOT EXISTS __DOMAIN___device_property (
                    device_id varchar(64) NOT NULL,
                    sub_domain varchar(32) NOT NULL,
                    channel varchar(64) NOT NULL,
                    batch varchar(64) NOT NULL,
                    create_time datetime DEFAULT NULL,
                    modify_time datetime DEFAULT NULL,
                    PRIMARY KEY (device_id)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
                """;

        public final static String CREATE_DEVICE_LICENSE_TABLE = """
                       CREATE TABLE IF NOT EXISTS __DOMAIN___device_licenses (
                       license               varchar(128) NOT NULL,
                       sub_domain            varchar(32) NOT NULL,
                       req_id                bigint NOT NULL,      -- licenseReqID which is used to generate the license
                       create_time           datetime NOT NULL,
                       PRIMARY KEY (license),
                       KEY (req_id)                                -- use the @req_id to search all associated licenses
                   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
                """;

        public final static String CREATE_DEVICE_FAILED_LICENSE_TABLE = """
                CREATE TABLE IF NOT EXISTS __DOMAIN___failed_device_licenses (
                    license               varchar(128) NOT NULL,
                    sub_domain            varchar(32) NOT NULL,
                    status                int NOT NULL,
                    used_time             bigint NOT NULL,
                    create_time           datetime NOT NULL,
                    PRIMARY KEY (license)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
                """;

        public final static String CREATE_DEVICE_ATOM_TABLE = """
                CREATE TABLE IF NOT EXISTS __DOMAIN___device_atom (
                		logical_id bigint(20) NOT NULL AUTO_INCREMENT,
                		sub_domain varchar(32) NOT NULL,
                		physical_id varchar(64) NOT NULL,
                		create_time datetime DEFAULT NULL,
                		modify_time datetime DEFAULT NULL,
                		PRIMARY KEY (logical_id),
                		UNIQUE KEY (sub_domain, physical_id)
                	) ENGINE=InnoDB DEFAULT CHARSET=utf8;
                 """;
    }


    public static class Product {
        public final static String CANNOT_BE_EMPTY = " ";
    }

    public static class Platform {
        public final static Long ID_NOT_FOUND = -1L;
        public final static int STATUS_ENABLE = 0;
        public final static int STATUS_DISABLE = 1;
    }

    public static class DS {
        public final static String PRODUCT = "product";
        public final static String PLATFORM = "platform";
        public final static String WAREHOUSE = "warehouse";

    }

    public static class OldDays {
        public final static int PRO_STATUS_NIL     = 0;
        public final static int PRO_STATUS_ENCRYPT = 1;

        public final static int NORMAL = 0;
        public final static int MASTER = 1;

        public final static int NVALID  = 0;
        public final static int ACTIVE   = 1;

        public final static int NIL   = 0;
        public final static int BATCH = 1;       // Different device use different private/public key.
        public final static int UNIFY = 2;       // All the device use the same private/public key.
    }

    public static class ZC {
        public static class HEAHERS{
            public static String HEADER_ZC_DOMAIN = "x-zc-major-domain-id";
            public static String HEADER_ZC_SUBDOMAIN = "x-zc-sub-domain-id";
            public static String HEADER_ZC_TIMEOUT = "X-Zc-Timeout";
            public static String HEADER_ZC_DEVELOPER_ID = "x-zc-developer-id";
        }

        public static int ZC_HS_SESSION_KEY_LEN = 16;
    }

    public static class ProductInfos {
        public static class MsgTransType{
            public static int TT = 1;
            public static int HIMIT = 2;
            public static int ALL = 3;
        }

        public static class OneMachineOneSecret{
            public static int NO = 1;
            public static int YES = 2;
        }

        public static int DEFAULT_DEVICE_TYPE = 1;


        public static int ZC_HS_SESSION_KEY_LEN = 16;
    }
}
